package com.hangman;

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class GameOptions extends ListActivity {
	private ArrayList<Category> categories;
	private String chosenLanguage = "";
	private int chosenCategory;
	private boolean soundState;
	private SoundPlayer sounds;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_picker);

		sounds = new SoundPlayer(getBaseContext());

		chosenLanguage = getIntent().getStringExtra("chosenLanguage");
		soundState = getIntent().getBooleanExtra("soundState", true);
		categories = new ArrayList<Category>();

		TextView headline = (TextView) findViewById(R.id.pick_item_text);
		headline.setTextColor(Color.WHITE);

		if (chosenLanguage.equalsIgnoreCase(getString(R.string.english))) {
			headline.setText(R.string.category_en);
		} else
			headline.setText(R.string.category_no);

		parseXML();

		setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1,
				getStringVersionOfCategories()));
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		String category_string = (String) getListAdapter().getItem(position);
		for (Category cat : categories) {
			if (cat.getName().equalsIgnoreCase(category_string.substring(0, category_string.lastIndexOf(":")))) {
				chosenCategory = categories.indexOf(cat);
			}
		}

		if (soundState)
			sounds.bubblepop();
		Intent gameInstance = new Intent("com.hangman.GAMEACTIVITY");
		gameInstance.putExtra("category", (categories.get(chosenCategory)));
		startActivity(gameInstance);
	}

	private void parseXML() {
		try {
			InputStream input = getBaseContext().getResources()
					.openRawResource(R.raw.words);
			SAXParserFactory myParserFactory = SAXParserFactory.newInstance();
			SAXParser myParser = myParserFactory.newSAXParser();
			XMLReader reader = myParser.getXMLReader();
			XMLHandler handler = new XMLHandler(reader, chosenLanguage);
			reader.setContentHandler(handler);
			reader.parse(new InputSource(input));

			categories = handler.getCategories();

		} catch (Exception e) {
			Log.i("parseXML", e.toString());
		}

	}

	private ArrayList<String> getStringVersionOfCategories() {
		ArrayList<String> result = new ArrayList<String>();
		for (Category categorie : categories) {
			result.add(categorie.toString());
		}
		return result;
	}

	
	public boolean onCreateOptionsMenu(Menu meny) {
		super.onCreateOptionsMenu(meny);
		meny.add("Exit");
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getTitle().equals("Exit")) {
			Intent intent = new Intent(getApplicationContext(), LanguagePicker.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intent.putExtra("doExit", true);
			startActivity(intent);
		}
		return true;
	}
}
